#include <unistd.h> 
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

    // 打开文件  读   打开一个文件  写  创建
    int fd_str = open("/home/bsh/xiyouji.txt", O_RDONLY);
    if (fd_str == -1)
    {
        perror("打开文件失败！");
        return -1;
    }
    int fd_dest = open("/home/bsh/xiyouji_bsh.txt", O_WRONLY | O_CREAT | O_APPEND, 0644);    
    if (fd_dest == -1)
    {
        perror("打开文件失败！");
        return -1;
    }
    
    // 循环读取该文件的内容
    char buf[1024] = {0};
    int ret_size;
    while ((ret_size = read(fd_str, buf, sizeof(buf))) > 0)
    {
        ssize_t write_size = write(fd_dest,buf, ret_size);
        if (write_size == -1)
        {
            perror("写入文件失败！");
            return -1;
        }
    }

    // 关闭文件
    close(fd_str);
    close(fd_dest);
    return 0;
}